package org.testis.gui.mvc.view.testdesign;

import org.testis.gui.mvc.view.*;
import org.testis.gui.mvc.controller.testdesign.LoadTestSuiteController;
import org.testis.gui.mvc.controller.TestDesignControllerList;
import org.testis.gui.mvc.utilities.Validatable;
import org.testis.gui.mvc.utilities.ValidationUtils;
import java.awt.Frame;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author  Cahit Emir Erel
 */
public class LoadTestSuiteView extends AtofBaseDialog implements
        AbstractViewPanel, Validatable {

    private static final long serialVersionUID = 123122L;

    public LoadTestSuiteView(Frame parent,
            boolean modal) {
        super(parent,
                modal);
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panel1 = new java.awt.Panel();
        propertyWindow = new java.awt.TextArea();
        suiteLabel = new javax.swing.JLabel();
        suiteChoice = new java.awt.Choice();
        openButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Open Test Suite");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        propertyWindow.setEditable(false);

        suiteLabel.setFont(new java.awt.Font("Dialog", 0, 11));
        suiteLabel.setText("Suites Found:");

        suiteChoice.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                suiteChoiceSelected(evt);
            }
        });

        openButton.setText("Open Test Suite");
        openButton.setEnabled(false);
        openButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openButtonActionPerformed(evt);
            }
        });

        jButton1.setText("Exit");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
        panel1.setLayout(panel1Layout);
        panel1Layout.setHorizontalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(propertyWindow, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addComponent(suiteLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(suiteChoice, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(openButton)))
                .addContainerGap())
        );
        panel1Layout.setVerticalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addComponent(suiteLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(15, 15, 15))
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addComponent(suiteChoice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(propertyWindow, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(openButton)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void suiteChoiceSelected(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_suiteChoiceSelected

        TestDesignControllerList.newInstance().getLoadTestSuiteController().
                operationSelectTestSuite(suiteChoice.getSelectedItem());
        if (suiteChoice.getSelectedItem().equals("")) {
            openButton.setEnabled(false);
        } else {
            openButton.setEnabled(true);
        }
}//GEN-LAST:event_suiteChoiceSelected

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed

        WindowListener[] listeners = this.getWindowListeners();
        for (int i = 0; i < listeners.length; i++) {
            this.removeWindowListener(listeners[i]);
        }
        this.setEnabled(false);
        TestDesignControllerList.newInstance().getLoadTestSuiteController().
                operationCloseWindow();
        System.gc();
    }//GEN-LAST:event_formWindowClosed

    private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
        openButton.setEnabled(false);
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                TestDesignControllerList.newInstance().getLoadTestSuiteController().operationLoad();
            }
        });
        this.dispose();
    }//GEN-LAST:event_openButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton openButton;
    private java.awt.Panel panel1;
    private java.awt.TextArea propertyWindow;
    private java.awt.Choice suiteChoice;
    private javax.swing.JLabel suiteLabel;
    // End of variables declaration//GEN-END:variables

    public void modelPropertyChange(PropertyChangeEvent evt) {
        //System.out.println(this.getClass() + evt.getPropertyName());
        if (evt.getPropertyName().equals(LoadTestSuiteController.SELECT_SUITE)) {
            if (suiteChoice.getItemCount() == 0) {
                suiteChoice.addItem(evt.getNewValue().toString());
            }
            suiteChoice.select(evt.getNewValue().toString());
        } else if (evt.getPropertyName().equals(
                LoadTestSuiteController.SHOW_LOADER)) {
            String[] choices = TestDesignControllerList.newInstance().
                    getLoadTestSuiteController().getTestSuiteNames();
            if (choices != null && choices.length > 0) {
                suiteChoice.add("");
                for (int i = 0; i < choices.length; i++) {
                    suiteChoice.addItem(choices[i]);
                }
            }
            this.centerParent();
        } else if (evt.getPropertyName().equals(
                LoadTestSuiteController.SHOW_DESCRIPTION)) {
            propertyWindow.setText(evt.getNewValue().toString());
        }
    }

    public boolean validateForm() {
        boolean valid = true;
        StringBuilder builder = new StringBuilder();
        if (ValidationUtils.isEmpty(suiteChoice.getSelectedItem())) {
            builder.append("Select a Test Suite!\n");
            valid = false;
        }
        if (!valid) {
            JOptionPane.showMessageDialog(null,
                    builder.toString(),
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
        return valid;
    }
}
